package com.qingcheng.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.qingcheng.dao.TbSpecMapper;
import com.qingcheng.pojo.goods.TbSpec;
import com.qingcheng.pojo.goods.TbTemplate;
import com.qingcheng.service.TbSpecService;
import com.qingcheng.service.TbTemplateService;
import com.qingcheng.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Simon
* @date 2020/07/27
*/
@Service
public class TbSpecServiceImpl implements TbSpecService {

    @Resource
    private TbSpecMapper tbSpecMapper;

    @Resource
    private TbTemplateService tbTemplateService;

    @Override
    public List<TbSpec> queryByI(Integer id) {
        Example example = new Example(TbSpec.class);
        Example.Criteria criteria = example.createCriteria();
        if(id>0)
            criteria.andEqualTo("templateId",id);
        List<TbSpec> tbSpecs = tbSpecMapper.selectByExample(example);
        return tbSpecs;
    }

    @Override
    public List<TbSpec> queryAll() {
        List<TbSpec> tbSpec = tbSpecMapper.selectAll();
            return tbSpec;
    }

    @Override
    public PageUtil<TbSpec> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSpec> pages = (Page<TbSpec>) tbSpecMapper.selectAll();
        PageUtil<TbSpec> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbSpec> pageQueryByAll(Integer pageNum, Integer pageSize, TbSpec tbSpec) {

        Example example=new Example(TbSpec.class);
        Example.Criteria criteria = example.createCriteria();
        if(tbSpec != null) {
            if(!StringUtils.isEmpty(tbSpec.getTemplateId())){
                criteria.andEqualTo("templateId",tbSpec.getTemplateId());
            }
            if(!StringUtils.isEmpty(tbSpec.getName())){
               criteria.andLike("name","%"+tbSpec.getName()+"%");
            }
            if(!StringUtils.isEmpty(tbSpec.getOptions())){
                criteria.andLike("options","%"+tbSpec.getOptions()+"%");
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSpec> pages = (Page<TbSpec>) tbSpecMapper.selectByExample(example);
        PageUtil<TbSpec> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    @Transactional
    public boolean saveTbSpec(TbSpec tbSpec) {
        // 新增时计算笛卡尔积必须的参数，但是前端并没有传入整个参数
        tbSpec.setProp("prop"+Math.round(Math.random()*100));
        int insert = tbSpecMapper.insert(tbSpec);
        if(insert>0){
            TbTemplate tempalte = tbTemplateService.findById(tbSpec.getTemplateId());
            tempalte.setSpecNum(tempalte.getSpecNum()+1);
            tbTemplateService.updateTbTemplate(tempalte);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbSpec(TbSpec tbSpec) {
        int i = tbSpecMapper.updateByPrimaryKeySelective(tbSpec);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        TbSpec tbSpec = this.tbSpecMapper.selectByPrimaryKey(id);
        TbTemplate byId = this.tbTemplateService.findById(tbSpec.getTemplateId());
        byId.setSpecNum(byId.getSpecNum()-1);
        this.tbTemplateService.updateTbTemplate(byId);
        int i = tbSpecMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbSpec findById(Integer id) {
        TbSpec tbSpec = tbSpecMapper.selectByPrimaryKey(id);
        return tbSpec;
    }
}
